<?php
declare(strict_types=1);

namespace App\Service;

use App\Helper\Log;
use App\Helper\Util;
use Hyperf\DbConnection\Db;
use GuzzleHttp\Client;

/**
 * 微信模板消息服务
 */
class WxTemplateMessageService 
{
    private $appid = "";
    private $appsecret = "";
    /**
     * [__construct description] 构造函数
     */
    public function __construct()
    {
        $wxconfig = config('wxconfig', []);
        $gzhconfig = $wxconfig['gzh']??[];
        $this->appid = $gzhconfig['appid']??NULL;
        $this->appsecret = $gzhconfig['appsecret']??NULL;
    }
    /**
     * 通知用户排班新增
     * [noticeUserTradAdd description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function noticeUserTradAdd($params)
    {
        $openid = $params['openid']??"";
        $cap = $params['cap']??"";
        $office = $params['office']??"";
        $content = $params['content']??"";

        $access_token = Util::getWxAccessToken($this->appid, $this->appsecret);
        $tpl_id = 'W48dsLWMBERTNxBQ7deoJ02znmUBmevW5wp4G-6t2gw';
        $miniprogram_appid = $this->getMiniProgramAppid();
        $pagepath = "/pages/index/index";
        $data = [
            'time3' => ['value' => $content],
            'thing7' => ['value' => $office],
            'thing15' => ['value' => $cap]
        ];
        $send_data = [
            'touser' => $openid, 
            'template_id' => $tpl_id, 
            // 'miniprogram' => [
            //     'appid'=>$miniprogram_appid, 
            //     'pagepath'=>$pagepath
            // ],
            'data' => $data
        ];
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
        $client = new Client();
        $response = $client->request("post", $url, [
            'headers' => [
                'Content-Type'=>'application/json',
            ],
            'body' => json_encode($send_data)
        ]);
        $res = $response->getBody()->getContents();
        $log_info = [
            'params'=>$params,
            'result'=>$res
        ];
        $log_info = json_encode($log_info,JSON_UNESCAPED_UNICODE);
        Log::get("wx_send_template_message")->info($log_info);
        return $res;
    }
    /**
     * 通知用户排班变更
     * [noticeUserTradChange description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function noticeUserTradChange($params)
    {
        $openid = $params['openid']??"";
        $cap = $params['cap']??"";
        $office = $params['office']??"";
        $wdate = $params['wdate']??"";
        $ori_text = $params['ori_text']??"";
        $now_text = $params['now_text']??"";

        $access_token = Util::getWxAccessToken($this->appid, $this->appsecret);
        $tpl_id = 'Be2vpaXW6CZisIibQvBX19vIlzo8UYfbiMgxvmRHKF0';
        $miniprogram_appid = $this->getMiniProgramAppid();
        $pagepath = "/pages/index/index";
        $data = [
            'thing8' => ['value' => "排班变更通知"],
            'time11' => ['value' => $wdate],
            'thing18' => ['value' => $ori_text],
            'thing15' => ['value' => $now_text],
            'thing22' => ['value' => $cap]
        ];
        $send_data = [
            'touser' => $openid, 
            'template_id' => $tpl_id, 
            // 'miniprogram' => [
            //     'appid'=>$miniprogram_appid, 
            //     'pagepath'=>$pagepath
            // ],
            'data' => $data
        ];
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
        $client = new Client();
        $response = $client->request("post", $url, [
            'headers' => [
                'Content-Type'=>'application/json',
            ],
            'body' => json_encode($send_data)
        ]);
        $res = $response->getBody()->getContents();
        $log_info = [
            'params'=>$params,
            'result'=>$res
        ];
        $log_info = json_encode($log_info,JSON_UNESCAPED_UNICODE);
        Log::get("wx_send_template_message")->info($log_info);
        return $res;
    }
    /**
     * 获取小程序APPID
     * [getMiniProgramAppid description]
     * @return [type] [description]
     */
    public function getMiniProgramAppid()
    {
        $wxconfig = config('wxconfig', []);
        $programconfig = $wxconfig['mini_program']??[];
        $programappid = $programconfig['appid']??NULL;
        return $programappid;
    }
}
